package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
//    添加用户功能
    @PostMapping("insert")

    public String insertUser(InsertDTO insertDTO) {
        User user = new User();
        BeanUtils.copyProperties(insertDTO, user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "添加成功！";
    }

    //    查询用户列表功能
    @GetMapping("userList")

    public List<UserListVO> userList() {
        return userMapper.userList();
    }
//删除用户功能
    @GetMapping("delete")

    public String deleteUser(Integer id){
        userMapper.deleteUser(id);
        return "删除成功！";
    }

//    修改用户功能
    @PostMapping("update")

    public String updateUser(User user){
        userMapper.updateUser(user);
        return "修改成功！";
    }
    @GetMapping("select/{id}")
    public UserVO selectUser(@PathVariable Integer id){
        return userMapper.select(id);
    }
}
